/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.controllers;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import test.controllers.exceptions.NonexistentEntityException;
import test.controllers.exceptions.PreexistingEntityException;
import test.entities.Trader;
import test.entities.Grade;
import test.entities.TraderGrade;
import test.entities.TraderGradePK;

/**
 *
 * @author Issam
 */
public class TraderGradeJpaController implements Serializable {

    public TraderGradeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TraderGrade traderGrade) throws PreexistingEntityException, Exception {
        if (traderGrade.getTraderGradePK() == null) {
            traderGrade.setTraderGradePK(new TraderGradePK());
        }
        traderGrade.getTraderGradePK().setIdGrade(traderGrade.getGrade().getIdGrade());
        traderGrade.getTraderGradePK().setMatricule(traderGrade.getTrader().getMatricule());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Trader trader = traderGrade.getTrader();
            if (trader != null) {
                trader = em.getReference(trader.getClass(), trader.getMatricule());
                traderGrade.setTrader(trader);
            }
            Grade grade = traderGrade.getGrade();
            if (grade != null) {
                grade = em.getReference(grade.getClass(), grade.getIdGrade());
                traderGrade.setGrade(grade);
            }
            em.persist(traderGrade);
            if (trader != null) {
                trader.getTraderGradeCollection().add(traderGrade);
                trader = em.merge(trader);
            }
            if (grade != null) {
                grade.getTraderGradeCollection().add(traderGrade);
                grade = em.merge(grade);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTraderGrade(traderGrade.getTraderGradePK()) != null) {
                throw new PreexistingEntityException("TraderGrade " + traderGrade + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TraderGrade traderGrade) throws NonexistentEntityException, Exception {
        traderGrade.getTraderGradePK().setIdGrade(traderGrade.getGrade().getIdGrade());
        traderGrade.getTraderGradePK().setMatricule(traderGrade.getTrader().getMatricule());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TraderGrade persistentTraderGrade = em.find(TraderGrade.class, traderGrade.getTraderGradePK());
            Trader traderOld = persistentTraderGrade.getTrader();
            Trader traderNew = traderGrade.getTrader();
            Grade gradeOld = persistentTraderGrade.getGrade();
            Grade gradeNew = traderGrade.getGrade();
            if (traderNew != null) {
                traderNew = em.getReference(traderNew.getClass(), traderNew.getMatricule());
                traderGrade.setTrader(traderNew);
            }
            if (gradeNew != null) {
                gradeNew = em.getReference(gradeNew.getClass(), gradeNew.getIdGrade());
                traderGrade.setGrade(gradeNew);
            }
            traderGrade = em.merge(traderGrade);
            if (traderOld != null && !traderOld.equals(traderNew)) {
                traderOld.getTraderGradeCollection().remove(traderGrade);
                traderOld = em.merge(traderOld);
            }
            if (traderNew != null && !traderNew.equals(traderOld)) {
                traderNew.getTraderGradeCollection().add(traderGrade);
                traderNew = em.merge(traderNew);
            }
            if (gradeOld != null && !gradeOld.equals(gradeNew)) {
                gradeOld.getTraderGradeCollection().remove(traderGrade);
                gradeOld = em.merge(gradeOld);
            }
            if (gradeNew != null && !gradeNew.equals(gradeOld)) {
                gradeNew.getTraderGradeCollection().add(traderGrade);
                gradeNew = em.merge(gradeNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                TraderGradePK id = traderGrade.getTraderGradePK();
                if (findTraderGrade(id) == null) {
                    throw new NonexistentEntityException("The traderGrade with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(TraderGradePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TraderGrade traderGrade;
            try {
                traderGrade = em.getReference(TraderGrade.class, id);
                traderGrade.getTraderGradePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The traderGrade with id " + id + " no longer exists.", enfe);
            }
            Trader trader = traderGrade.getTrader();
            if (trader != null) {
                trader.getTraderGradeCollection().remove(traderGrade);
                trader = em.merge(trader);
            }
            Grade grade = traderGrade.getGrade();
            if (grade != null) {
                grade.getTraderGradeCollection().remove(traderGrade);
                grade = em.merge(grade);
            }
            em.remove(traderGrade);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TraderGrade> findTraderGradeEntities() {
        return findTraderGradeEntities(true, -1, -1);
    }

    public List<TraderGrade> findTraderGradeEntities(int maxResults, int firstResult) {
        return findTraderGradeEntities(false, maxResults, firstResult);
    }

    private List<TraderGrade> findTraderGradeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(TraderGrade.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TraderGrade findTraderGrade(TraderGradePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TraderGrade.class, id);
        } finally {
            em.close();
        }
    }

    public int getTraderGradeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<TraderGrade> rt = cq.from(TraderGrade.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
